
//
//  HomeSearchTextView.m
//  FD
//
//  Created by xiaofang wang on 2018/11/28.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  首页查看配送费范围，,搜索输入框。
//  每月租金:11255

#import "HomeSearchTextView.h"
@interface HomeSearchTextView()<UITextFieldDelegate>

/**图标*/
@property (nonatomic, strong) UIImageView *iconIv;
/**2.输入框*/
@property (nonatomic, strong) UITextField *textField;

@end

@implementation HomeSearchTextView
#pragma mark - 一 init
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
        self.backgroundColor = [UIColor whiteColor]; //白色
    }
    return self;
}

- (void)setupView {
    [self addSubview:self.iconIv];
    [self addSubview:self.textField];
}

#pragma mark - 二 getter
- (UIImageView *)iconIv {
    if (!_iconIv) {
        _iconIv = [[UIImageView alloc] init];
        _iconIv.image = [UIImage imageNamed:@"address"];
    }
    return _iconIv;
}

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.textAlignment = NSTextAlignmentCenter;
//        _textField.placeholder = @"定位中...";
        _textField.delegate = self;
        //灰色 圆角
        _textField.backgroundColor = rgba(244, 244, 244, 1.0);
        _textField.layer.masksToBounds = YES;
        _textField.layer.cornerRadius = 10;
        
        //添加点击事件
        [_textField addTarget:self action:@selector(fdtextFieldDidChange:) forControlEvents:(UIControlEventEditingChanged)];
    }
    return _textField;
}


#pragma mark - 三 layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.iconIv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.size.mas_equalTo(CGSizeMake(18, 23));
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    //设置高度。默认d高度太小了。
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconIv.mas_right).offset(5);
        make.right.equalTo(self.mas_right).offset(-20);
        make.top.equalTo(self.mas_top).offset(5);
        make.bottom.equalTo(self.mas_bottom).offset(-5);
    }];
}

#pragma mark - 四  代理方法 UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
 
    if ([self.delegate respondsToSelector:@selector(homesearchTextViewDidBeginEditing:)]) {
        [self.delegate homesearchTextViewDidBeginEditing:textField];
    }
}


//dismiss the result Vc
- (void)textFieldDidEndEditing:(UITextField *)textField {
   
    if ([self.delegate respondsToSelector:@selector(homesearchTextViewDidEndEditing:)]) {
        [self.delegate homesearchTextViewDidEndEditing:textField];
    }
}

- (void)fdtextFieldDidChange:(UITextField *)tf {
    if ([self.delegate respondsToSelector:@selector(homesearchTextFieldDidChange:)]) {
        [self.delegate homesearchTextFieldDidChange:tf];
    }
}


#pragma mark - 五 接口
//回收键盘
- (void)homesearchViewResignFirstResponser {
 
    [self.textField resignFirstResponder];
}

//更新输入框文字。
- (void)homesearchViewUpdateTFStr:(NSString *)str placeholder:(NSString *)placeholder {
    self.textField.placeholder = placeholder;
    
    if (![NSString isEmptyString:str]) {
        self.textField.text = str;
    }
}
@end

